class Solution 
{
    int ret=0;
public:
    int reversePairs(vector<int>& record) 
    {
        vector<int>help(record.size());
       return  mergesort(record,0,record.size()-1,help);

    }
    int mergesort(vector<int>&nums,int left,int right,vector<int>&help)
    {
        if(left>=right) return 0;
        int mid=(right-left)/2+left;
        int ret=0;
        ret+=mergesort(nums,left,mid,help);
        ret+=mergesort(nums,mid+1,right,help);
      
        int begin1=left,end1=mid,begin2=mid+1,end2=right,index=left;
        while(begin1<=end1&&begin2<=end2)
        {
            if(nums[begin1]<=nums[begin2]) help[index++]=nums[begin1++];
            else 
            {
                ret+=mid-begin1+1;
                help[index++]=nums[begin2++];
            }

        }
        while(begin1<=end1) help[index++]=nums[begin1++];
        while(begin2<=end2) help[index++]=nums[begin2++];
        for(int i=left;i<=right;i++) nums[i]=help[i];
        return ret;
    }
};